<template>
  <div>
    <!-- 大客户订单提报 -->
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      :labelWidth="110"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjOrderInquirys"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    >

    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher :rowData="formData" @close="dealercloseHandler" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="iscontract"
      :title="'选择合同'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <contract :rowData="formData" @close="contractClose" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="isProject"
      :title="'选择项目'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <AccountProject :rowData="formData" @close="ProjectClose" />
    </BaseDialog>

    <Drawer
      :visible.sync="detailVisible"
      :title="'大客户订单信息'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
      width="72%"
    >
      <detailModel  :rowData="formData" @close="deailClose" />
    </Drawer>
  </div>
</template>
<script>
import {getList,getNotPage} from '@/api/omSale/KeyCustomers/OrderReview'
import { dictionary } from '@/utils/dictMixin';
import detailModel from './detailModel'
import AccountProject from './AccountProject'
import contract from './contract'
import ownerSeacher from './ownerSeacher'
import moment from 'moment'
export default {
  name:'TemplateDistribution',
  data(){
    this.BaseTable=null;
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      fetchParams:{
        majorType:2,
        saveType:'OEM',
        searchType:2
      },
      fetchApi:getList,
      formData:{},
      detailVisible:false,
      iscontract:false,
      isProject:false,
      isDiscount:false,
      issearch:false,
      emitVisible:false,
      contractId:'',
      dealerCode:'',
      ProjectId:''
    }
  },
  mixins:[dictionary],
  components: {
    ownerSeacher,
    detailModel,
    AccountProject,
    contract,
  },
  mounted(){
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created(){
    this.getNotPage(60061002)
  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请选择',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(3)}></el-button>
              </div>
            );
          },
          change:p=>{
            if(!p){
              this.dealerCode=''
            }
          },
        },
        {
          type: 'INPUT',
          label: '合同名称',
          fieldName: 'majorContractName',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change:p=>{
            if(!p){
              this.contractId=''
            }
          },
        },
        {
          type: 'INPUT',
          label: '大客户项目名称',
          fieldName: 'majorProjectName',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change:p=>{
            if(!p){
              this.ProjectId=''
            }
          },
        },

        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList:[
            {text:'直销大客户订单',value:60061002},
            {text:'区域大客户订单',value:60061003},
            {text:'代交车大客户订单',value:60061004}
          ],
          initialValue:60061002,
          change:p=>{
            if(p){
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').itemList=[];
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').initialValue='';
              this.getNotPage(p)
            }else{
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').itemList=[];
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').initialValue='';
            }
          }
        },
        {
          type: 'SELECT',
          label: '订单细分类型',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList:[],
        },
        {
          type: 'INPUT',
          label: '大客户名称',
          fieldName: 'majorCustomerName',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList:this.createDictList('6043')
        },
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入',
        },
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'reportStartDate|reportEndDate',
          placeholder: '请选择',
          initialValue:[],
          dateType:'exactdaterange'
        },

      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 120,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button  size="mini" type="text" onClick={() => this.editHandle(props.row,1)}>
                  审核
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button  size="mini" type="text" onClick={() => this.editHandle(props.row,2)}>
                  详情
                </multiuse-button>

              </div>
            );
          }
        },
        {
          title: '合同名称',
          dataIndex: 'majorContractName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '大客户项目名称',
          dataIndex: 'majorProjectName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '大客户名称',
          dataIndex: 'majorCustomerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: (p) => {
            return <span >{this.createDictText(p.row.orderType, '6006')}</span>;
          },
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6043'),
          render: (p) => {
            return <span >{this.createDictText(p.row.orderStatus, '6043')}</span>;
          },
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6004'),
          render: (p) => {
            return <span >{this.createDictText(p.row.transportType, '6004')}</span>;
          },
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '提报数量',
          dataIndex: 'orderTotalNum',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '要求交付日期',
          dataIndex: 'requiredDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单总金额',
          dataIndex: 'beforeTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '折扣总金额',
          dataIndex: 'discountTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: (p) => {
            return <span >{this.createDictText(p.row.isRebate, '1004')}</span>;
          },
        },
        {
          title: '返利使用总金额',
          dataIndex: 'rebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '单车返利总金额',
          dataIndex: 'singleRebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '建店返利总金额',
          dataIndex: 'buildRebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '实付总金额',
          dataIndex: 'realPayTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '审批数量',
          dataIndex: 'checkNum',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span >{p.row.checkNum?p.row.checkNum:0}</span>;
          },
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '创建人',
          dataIndex: 'createdName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
      ]
    },
    selectChangeHandle(row){

    },
    seacherFun(type){
      if(type==1){
        this.iscontract=true;
      }else if(type==2){
        this.isProject=true;
      }else{
        this.issearch=true;
      }
    },
    addHandle(data){
      this.detailVisible=true
    },
    changeHandle(params){
      this.fetchParams.majorContractNo=this.contractId;
      this.fetchParams.projectCode=this.ProjectId;
      this.fetchParams.dealerCode=this.dealerCode;
      this.fetchParams=Object.assign({},this.fetchParams,params)
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async editHandle(data,type){
      if(type==1){
        this.formData=data;
        this.emitVisible=true;
      }else if(type==2){
        this.formData=data;
        this.detailVisible=true;
      }

    },
    async getNotPage(orderType){
      let list=await getNotPage({orderType:orderType})
        this.topFilterList.map(x=>{
          if(x.fieldName =='orderDetailType'){
            x.itemList=list.data.map(x=>({text:x.subName,value:x.subId}))
          }
        })
    },
    selectChangeHandle(val){

    },
    emitcloseHandler(val){

      if(val){
        this.fetchParams.majorContractNo=this.contractId;
        this.fetchParams.projectCode=this.ProjectId;
        this.fetchParams.dealerCode=this.dealerCode;
        this.fetchParams=Object.assign({},this.fetchParams)
      }
      this.emitVisible=false
    },
    deailClose(){
      this.detailVisible=false;
    },
    // 合同返回
    contractClose(val){
      if(val){
        this.topFilterList.map(x=>{
          if(x.fieldName=='majorContractName'){
            x.initialValue=val.contractName;
            this.contractId=val.contractNum
          }
        })

      }
      this.iscontract=false;
    },
    ProjectClose(val){
      if(val){
        this.topFilterList.map(x=>{
          if(x.fieldName=='majorProjectName'){
            x.initialValue=val.projectName;
            this.ProjectId=val.projectCode;
          }
        })
      }
      this.isProject=false;
    },
    dealercloseHandler(val){
      if(val){
        this.topFilterList.find(x=>x.fieldName=='dealerName').initialValue=val.dealerName;
        this.dealerCode=val.dealerCode;

      }
      this.issearch=false;
    },
  }
}
</script>
